<?php
namespace app\common\validate\jy;

class PickUpCode extends Base {
	/**
     * 验证规则
     */
    protected $rule = [
        'id'           => 'require|isUser:jy\\Order|pickUp',
        'code'         => 'require|code|isRefund'
    ];
    /**
     * 提示消息
     */
    protected $message = [
        'id.require'     => '订单ID不能为空',
        'id.isUser'      => '订单不属于您',
        'id.pickUp'      => '订单不处于待提货状态',
        'code.require'   => '提货码不能为空',
        'code.code'      => '提货码不正确或已经超时',
        'code.isRefund'  => '该订单正在退货中'
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'index' => ['id'],
        'pick_up'  => ['id', 'code']
    ];

    //订单不处于待提货状态
    public function pickUp($value, $rule, $data = []) {
        $order = loader()->loadAny('only', 'jy\\Order')->find('jy\\Order', $value);
        return $order && $order['status'] == loadEnum('OrderStatus')->code2key('daiziti');  
    }

    public function code($value, $rule, $data = []) {
        return loadTool('PickUpCode')->validate_code($data['id'], $data['code']);
    }

    //判断订单当前是否有退款单
    public function isRefund($value, $rule, $data = []) {
        $row = loadModel('jy\\OrderRefund')->where('oid', $data['id'])->whereNotIn('status', loadEnum('OrderRefundStatus')->over())->find();
        return empty($row);
    }
}
